package com.lkei.controller;

import com.alibaba.fastjson.JSONObject;
import com.lkei.dto.Result;
import com.lkei.entity.*;
import com.lkei.service.SchoolMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("schoolMessage")
public class SchoolMessageController {

    @Autowired
    private SchoolMessageService schoolMessageService;

    @CrossOrigin
    @PostMapping("addMessage")
    public Result addMessage(@RequestBody String data){
        // TODO 需要捕获消息标题和消息详情的长度异常
        // TODO 添加JWT验证

        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        // 取出map中的data,继续进行Json解析
        JSONObject jsonObject2 = JSONObject.parseObject(map.get("data").toString());
        Map<String, Object> map2 = new HashMap<>();
        map2.putAll(jsonObject2);

        // 声明接收人类型
        String receiver_type = map2.get("receiver_type").toString();

        // 如果接收人类型是教师
        if("教师".equals(receiver_type)){

            // 声明接收人的标志的类型
            String receive_sign_type = map2.get("receive_sign_type").toString();
            // 创建（校方-教师）消息对象和校方发布（A）对象
            SchoolTeacherMessage schoolTeacherMessage = new SchoolTeacherMessage();
            SchoolPublishA schoolPublishA = new SchoolPublishA();
            schoolTeacherMessage.setMessage_title(map2.get("message_title").toString());
            schoolTeacherMessage.setMessage_detail(map2.get("message_detail").toString());
            schoolTeacherMessage.setMessage_type(map2.get("message_type").toString());
            schoolTeacherMessage.setTime(map2.get("time").toString());
            schoolTeacherMessage.setPublisher_id(Integer.valueOf(map.get("account").toString()).intValue());
            schoolTeacherMessage.setReceiver_sign_type(receive_sign_type);
            schoolMessageService.addSchoolTeacherMessage(schoolTeacherMessage);

            // mapper设置自动增长后可以返回主键信息
            Integer messageID = schoolTeacherMessage.getId();
            schoolPublishA.setMessage_id(messageID);
            if("全体教师".equals(receive_sign_type)){


            }else if ("职称".equals(receive_sign_type)){

                schoolPublishA.setReceiver_sign(Integer.valueOf(map2.get("professional_title").toString()).intValue());

            }else if("院系部门".equals(receive_sign_type)){

                schoolPublishA.setReceiver_sign(Integer.valueOf(map2.get("department_id").toString()).intValue());
            }else if("职务".equals(receive_sign_type)){

                schoolPublishA.setReceiver_sign(Integer.valueOf(map2.get("post_id").toString()).intValue());
            }
            schoolMessageService.addSchoolPublishA(schoolPublishA);
        }else if("学生".equals(receiver_type)){

            // 创建（校方-学生）消息对象和 校方发布（B）对象
            SchoolStudentMessage schoolStudentMessage = new SchoolStudentMessage();
            SchoolPublishB schoolPublishB = new SchoolPublishB();
            schoolStudentMessage.setMessage_type(map2.get("message_type").toString());
            schoolStudentMessage.setMessage_title(map2.get("message_title").toString());
            schoolStudentMessage.setMessage_detail(map2.get("message_detail").toString());
            schoolStudentMessage.setTime(map2.get("time").toString());
            schoolStudentMessage.setPublisher_id(Integer.valueOf(map.get("account").toString()).intValue());
            // 将消息信息插入（校方-学生）消息表
            schoolMessageService.addSchoolStudentMessage(schoolStudentMessage);

            // 获取上一步返回的主键，并插入校方发布表（B）
            schoolPublishB.setMessage_id(schoolStudentMessage.getId());
            schoolPublishB.setDepartment_id(Integer.valueOf(map2.get("department_id").toString()).intValue());
            schoolPublishB.setGrade(map2.get("grade").toString());
            schoolMessageService.addSchoolPublishB(schoolPublishB);

        }
        return Result.INSERT_SUCCESS;
    }

    @CrossOrigin
    @PostMapping("findTask")
    public Result findTask(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        // 获取当前登录的账号
        int account = Integer.valueOf(map.get("account").toString()).intValue();
        List<MessageList> schoolTeacherMessage = schoolMessageService.findSchoolTeacherTask(account);
        for (MessageList message:schoolTeacherMessage) {
            message.setType(true);
            message.setReceiver("教师");
        }
        List<MessageList> schoolStudentTask = schoolMessageService.findSchoolStudentTask(account);
        for (MessageList message:schoolStudentTask) {
            message.setType(false);
            message.setReceiver("学生");
            // 将这个集合的元素添加到另一个集合里进行封装
            schoolTeacherMessage.add(message);
        }
        Collections.sort(schoolTeacherMessage);
        return Result.SUCCESS.setNewData(schoolTeacherMessage);
    }

    @CrossOrigin
    @PostMapping("findMessageDetail")
    public Result findMessageDetail(@RequestBody String data){
        // TODO 添加JWT验证

        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        // 获取携带的消息编号
        int messageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        String receiver = map.get("receiver").toString();
        String messageDetail = new String();
        if(receiver.equals("教师")){
           messageDetail = schoolMessageService.findSchoolTeacherMessageDetails(messageID);
        }
        else if(receiver.equals("学生")){
            messageDetail = schoolMessageService.findSchoolStudentMessageDetails(messageID);
        }
        return Result.SUCCESS.setNewData(messageDetail);
    }

    @CrossOrigin
    @PostMapping("deleteMessage")
    public Result deleteMessage(@RequestBody String data){
        // TODO 添加JWT验证

        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        // 获取携带的消息编号
        int messageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        String receiver = map.get("receiver").toString();
        if(receiver.equals("教师")){
            schoolMessageService.deleteSchoolTeacherMessage(messageID);
            schoolMessageService.deleteSchoolPublishA(messageID);
        }
        else if(receiver.equals("学生")){
            schoolMessageService.deleteSchoolStudentMessage(messageID);
            schoolMessageService.deleteSchoolPublishB(messageID);
        }
        return Result.DELETE_SUCCESS;
    }

    @CrossOrigin
    @PostMapping("findNotice")
    public Result findNotice(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        // 获取当前登录的账号
        int account = Integer.valueOf(map.get("account").toString()).intValue();
        List<MessageList> schoolTeacherMessage = schoolMessageService.findSchoolTeacherNotice(account);
        for (MessageList message:schoolTeacherMessage) {
            message.setType(true);
            message.setReceiver("教师");
        }
        List<MessageList> schoolStudentTask = schoolMessageService.findSchoolStudentNotice(account);
        for (MessageList message:schoolStudentTask) {
            message.setType(false);
            message.setReceiver("学生");
            // 将这个集合的元素添加到另一个集合里进行封装
            schoolTeacherMessage.add(message);
        }
        Collections.sort(schoolTeacherMessage);
        return Result.SUCCESS.setNewData(schoolTeacherMessage);
    }
}
